using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;

namespace ELportal.Common.Middlewares;

public class AuditHandlerMiddleware
{
	private readonly RequestDelegate next;
	private readonly ILogger logger;

	public AuditHandlerMiddleware(RequestDelegate next, ILogger<AuditHandlerMiddleware> logger)
	{
		this.next = next;
		this.logger = logger;
	}

	public async Task InvokeAsync(HttpContext httpContext)
	{
		var message = string.Join('t',
			DateTimeOffset.Now.ToString("o"),
			httpContext.User?.Identity?.Name ?? "[unknown]",
			httpContext.Request.Method,
			httpContext.Request.Path,
			httpContext.Request.Host);

		logger.LogInformation(message);

		await next(httpContext);
	}
}
